﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using BusinessObject;
using BusinessLogic;
using System.Web.Security;
using Utility;
namespace WebTeamplate
{
    public partial class masDefault : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            InitCheckMenu();

        }

        private void LoadAdminMenu()
        {

        }
        private void InitCheckMenu()
        {
            BasePage p = (BasePage)this.Page;
            Vw_aspnet_Roles objRole = BusinessLogic.UserProfileUtil.GetMaxRole(p.UserloginId, Membership.ApplicationName);
            if (objRole != null)
            {
                string key = "menuAdmin_" + objRole.RoleName;
                List<MenuResult> lstMenu = new List<MenuResult>();
                //get from cache
                //if (BizObject.Cache[key] != null)
                //{
                //    lstMenu = (List<MenuResult>)BizObject.Cache[key];
                //}
                //else
                //{
                string url = Server.MapPath("~/Admin/usercontrol/" + objRole.MenuFile);
                GetAdminMenu(ref lstMenu, p, url);
                //cache data
                BizObject.CacheData(key, lstMenu);
                //  }
                LeftMenu1.MenuList = lstMenu;
                LeftMenu1.currentURL = p.GetCurrentPageName();
            }
        }

        private void GetAdminMenu(ref List<MenuResult> lstSource, BasePage p, string url)
        {
            try
            {

                XmlDocument mDocument = new XmlDocument();
                mDocument.Load(url);
                XmlNode mCurrentNode = mDocument.DocumentElement;
                XmlNodeList nodeList = mCurrentNode.SelectNodes("//menus");
                foreach (XmlNode parent in nodeList)
                {
                    GetEachParent(ref lstSource, p, parent);
                }
            }
            catch
            {
            }
        }
        private void GetEachParent(ref List<MenuResult> lstSource, BasePage p, XmlNode root)
        {
            foreach (XmlNode node in root)
            {
                GetMenu(ref lstSource, p, node);
            }
        }
        private void GetMenu(ref List<MenuResult> lstSource, BasePage p, XmlNode root)
        {
            MenuResult objParent = new MenuResult();
            string isActive = string.Empty;
            isActive = GetNodeValue(root, "isActive");
            if (isActive != "01")
                return;
            objParent.Name = GetNodeValue(root, "name");
            objParent.Link = GetNodeValue(root, "link");
            objParent.Id = ConvertValueObject.ParseInt(GetNodeValue(root, "id"));
            objParent.ParentID = int.MinValue;
            lstSource.Add(objParent);
            bool isSelect = false;
            XmlDocument mDocument = new XmlDocument();
            mDocument.LoadXml("<child>" + root.InnerXml + "</child>");
            string name = string.Empty;
            string link = string.Empty;

            foreach (XmlNode node in mDocument.FirstChild)
            {
                MenuResult objMenu = new MenuResult();
                objMenu.Name = GetNodeValue(node, "name");
                objMenu.Link = GetNodeValue(node, "link");
                objMenu.ParentID = objParent.Id;
                if (GetNodeValue(node, "isActive") == "01")
                    objMenu.IsActive = true;
                else
                    objMenu.IsActive = false;
                if (objMenu.IsActive)
                {
                    if ("admin/" + link.Trim().ToLower() == p.GetCurrentPageUrl().Trim().ToLower())
                    {
                        objMenu.IsSelect = true;
                        isSelect = true;
                    }
                    else objMenu.IsSelect = false;
                }
                lstSource.Add(objMenu);
            }

            // return objMenu;
        }
        private string GetNodeValue(XmlNode node, string attribute)
        {
            try
            {
                return node.Attributes[attribute].Value.ToString();
            }
            catch
            {
            }
            return string.Empty;
        }
    }
}
